﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initUi();

    initAction();
    initMenuBar();
    initToolBar();
    initConnect();
    initStatusBar();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUi()
{
    m_pDashboardForm = new DashboardForm();
    centralWidget()->setParent(nullptr);
    this->setCentralWidget(m_pDashboardForm);
    /*
    m_pSplitter = new QSplitter(Qt::Horizontal, this);
    m_pTreeWidget = new QTreeWidget(this);

    m_pUserForm = new UserForm(this);

    m_pSplitter->addWidget(m_pTreeWidget);
    m_pSplitter->addWidget(m_userForm);
    m_pSplitter->setStretchFactor(2, 8);

    this->setCentralWidget(m_pSplitter);
    */
}

void MainWindow::initAction()
{
    m_pSysInfoAction = new QAction(QIcon(":/images/applications.png"),
                                   QString::fromLocal8Bit("系统信息"), this);
    m_pSysInfoAction->setStatusTip(QString::fromLocal8Bit("显示系统信息"));
    m_pReloginAction = new QAction(QIcon(":/images/arrow_refresh.png"),
                                   QString::fromLocal8Bit("重新登录(&r)"), this);
    m_pReloginAction -> setShortcut(QString("ctrl+r"));
    m_pMaxAction = new QAction(QIcon(":/images/application-resize-full.png"),
                               QString::fromLocal8Bit("最大化"), this);
    m_pMinAction = new QAction(QIcon(":/images/application-resize-actual.png"),
                               QString::fromLocal8Bit("最小化"), this);
    m_pNormalAction = new QAction(QIcon(":/images/application-resize.png"),
                                  QString::fromLocal8Bit("正常"), this);
    m_pExitAction = new QAction(QIcon(":/images/control-power.png"),
                                QString::fromLocal8Bit("退出(&x)"), this);
    m_pExitAction -> setShortcut(QString("ctrl+x"));



    m_pRoleAction = new QAction(QIcon(":/images/group.png"),
                                QString::fromLocal8Bit("角色"), this);
    m_pRoleAction->setStatusTip(QString::fromLocal8Bit("角色管理"));
    m_pUserAction = new QAction(QIcon(":/images/user.png"),
                                QString::fromLocal8Bit("用户"), this);
    m_pUserAction->setStatusTip(QString::fromLocal8Bit("用户管理"));
    m_pMenuAction = new QAction(QIcon(":/images/application.png"),
                                QString::fromLocal8Bit("菜单"), this);
    m_pMenuAction->setStatusTip(QString::fromLocal8Bit("菜单管理"));

    m_pAboutAction = new QAction(QIcon(":/images/question.png"),
                                 QString::fromLocal8Bit("关于"), this);
}

void MainWindow::initMenuBar()
{
    m_pSystemMenu = new QMenu(QString::fromLocal8Bit("系统(&s)"), this);
    m_pBasicMenu = new QMenu(QString::fromLocal8Bit("基本信息(&b)"), this);
    m_pHelpMenu = new QMenu(QString::fromLocal8Bit("帮助(&h)"), this);

    m_pSystemMenu -> addAction(m_pSysInfoAction);
    m_pSystemMenu -> addSeparator();
    m_pSystemMenu -> addAction(m_pReloginAction);
    m_pSystemMenu -> addSeparator();
    m_pSystemMenu -> addAction(m_pMaxAction);
    m_pSystemMenu -> addAction(m_pMinAction);
    m_pSystemMenu -> addAction(m_pNormalAction);
    m_pSystemMenu -> addAction(m_pExitAction);

    m_pBasicMenu -> addAction(m_pRoleAction);
    m_pBasicMenu -> addAction(m_pUserAction);
    m_pBasicMenu -> addSeparator();
    m_pBasicMenu -> addAction(m_pMenuAction);

    m_pHelpMenu -> addAction(m_pAboutAction);

    menuBar() -> addMenu(m_pSystemMenu);
    menuBar() -> addMenu(m_pBasicMenu);
    menuBar() -> addMenu(m_pHelpMenu);
}

void MainWindow::initToolBar()
{
    m_pSysToolBar = addToolBar(tr("sys"));
    m_pSysToolBar->addAction(m_pSysInfoAction);
    m_pSysToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    m_pBaseToolBar = addToolBar(tr("base"));
    m_pBaseToolBar->addAction(m_pRoleAction);
    m_pBaseToolBar->addAction(m_pUserAction);
    m_pBaseToolBar->addSeparator();
    m_pBaseToolBar->addAction(m_pMenuAction);
    m_pBaseToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
}

void MainWindow::initConnect()
{
    connect(m_pSysInfoAction, &QAction::triggered, this, [=](){
        if(m_pDashboardForm == nullptr)
        {
            m_pDashboardForm = new DashboardForm();
        }
        centralWidget()->setParent(nullptr);
        this->setCentralWidget(m_pDashboardForm);
    });
    connect(m_pReloginAction, &QAction::triggered, this, [=](){
        QString program = QApplication::applicationFilePath();
        QStringList arguments = QApplication::arguments();
        QString workingDirectory = QDir::currentPath();
        QProcess::startDetached(program, arguments, workingDirectory);
        QApplication::exit();
    });
    connect(m_pMaxAction, &QAction::triggered, this, &MainWindow::showMaximized);
    connect(m_pMinAction, &QAction::triggered, this, &MainWindow::showMinimized);
    connect(m_pNormalAction, &QAction::triggered, this, &MainWindow::showNormal);
    connect(m_pExitAction, &QAction::triggered, qApp, &QApplication::quit);

    connect(m_pRoleAction, &QAction::triggered, this, [=](){
        if(m_pRoleForm == nullptr)
        {
           m_pRoleForm = new RoleForm();
        }
        centralWidget()->setParent(nullptr);
        this->setCentralWidget(m_pRoleForm);
        m_pRoleForm->init();
    });
    connect(m_pUserAction, &QAction::triggered, this, [=](){
        if(m_pUserForm == nullptr)
        {
            m_pUserForm = new UserForm();
        }
        centralWidget()->setParent(nullptr);
        this->setCentralWidget(m_pUserForm);
        m_pUserForm->init();
    });
    connect(m_pMenuAction, &QAction::triggered, this, [=](){
        if(m_pMenuForm == nullptr)
        {
            m_pMenuForm = new MenuForm();
        }
        centralWidget()->setParent(nullptr);
        this->setCentralWidget(m_pMenuForm);
    });

    connect(m_pAboutAction, &QAction::triggered, this, [=](){
        QMessageBox::about(this,
                           QString::fromLocal8Bit("惠洽"),
                           QString::fromLocal8Bit("Qt demo"));
    });
}

void MainWindow::initStatusBar()
{
    m_pCurrentTimeLabel = new QLabel(this);
    m_pCurrentTimeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    ui->statusbar->addPermanentWidget(m_pCurrentTimeLabel);
    ui->statusbar->setStyleSheet(QString("QStatusBar::item{border:0px}"));

    m_pTimer = new QTimer(this);
    connect(m_pTimer, &QTimer::timeout, [=](){
        m_pCurrentTimeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    });
    m_pTimer->start(1000);
}
